package com.jyw.system.service.impl;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jyw.common.constant.Constants;
import com.jyw.common.utils.SecurityUtils;
import com.jyw.system.domain.SysUserRole;
import com.jyw.system.domain.vo.UserDeptRoleVO;
import org.springframework.stereotype.Service;
import com.jyw.system.mapper.SysUserDeptRoleMapper;
import com.jyw.system.domain.SysUserDeptRole;
import com.jyw.system.service.ISysUserDeptRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * 用户部门角色关联Service业务层处理
 * 
 * @author system
 * @date 2023-08-09
 */
@Service
public class SysUserDeptRoleServiceImpl extends ServiceImpl<SysUserDeptRoleMapper,SysUserDeptRole> implements ISysUserDeptRoleService
{
    @Resource
    private SysUserDeptRoleMapper sysUserDeptRoleMapper;

    /**
     * 新增用户部门角色关联
     * 
     * @param sysUserDeptRole 用户部门角色关联
     * @return 结果
     */
    @Override
    public int insertSysUserDeptRole(SysUserDeptRole sysUserDeptRole)
    {
        return sysUserDeptRoleMapper.insertSysUserDeptRole(sysUserDeptRole);
    }

    /**
     * 修改用户部门角色关联
     * 
     * @param sysUserDeptRole 用户部门角色关联
     * @return 结果
     */
    @Override
    public int updateSysUserDeptRole(SysUserDeptRole sysUserDeptRole)
    {
        return sysUserDeptRoleMapper.updateSysUserDeptRole(sysUserDeptRole);
    }

    /**
     * 批量删除用户部门角色关联
     * 
     * @param ids 需要删除的用户部门角色关联主键
     * @return 结果
     */
    @Override
    public int deleteSysUserDeptRoleByIds(Long[] ids)
    {
        return sysUserDeptRoleMapper.deleteSysUserDeptRoleByIds(ids);
    }

    /**
     * 删除用户部门角色关联信息
     * 
     * @param id 用户部门角色关联主键
     * @return 结果
     */
    @Override
    public int deleteSysUserDeptRoleById(Long id)
    {
        return sysUserDeptRoleMapper.deleteSysUserDeptRoleById(id);
    }

    @Override
    public int countUserDeptRoleByRoleId(Long roleId) {
        return sysUserDeptRoleMapper.countUserDeptRoleByRoleId(roleId);
    }

    /**
     * 获取用户默认的部门角色关系
     * @param userId
     * @return
     */
    @Override
    public List<SysUserDeptRole> getUserDefault(Long userId) {
        return sysUserDeptRoleMapper.selectList(
                new QueryWrapper<SysUserDeptRole>()
                        .eq(StrUtil.toUnderlineCase(SysUserDeptRole.Fields.userId), userId)
//                        .eq(StrUtil.toUnderlineCase(SysUserDeptRole.Fields.isDefault), Constants.BOOL_TRUE)
        );
    }

    @Override
    public List<UserDeptRoleVO> getUserDeptRoleInfo() {
        return sysUserDeptRoleMapper.getUserDeptRoleInfo(SecurityUtils.getUserId());
    }
}
